mui.init({});
var userinfo = app.getState() || {};
var a1_money=0;
refresh_data();

mui.plusReady(function() {
	//下载安装
  	document.getElementById('btnCkq').addEventListener('tap', function() {
  		plus.runtime.openURL('http://agent.xunmaigou.cn/app/cqbao/');
  	});
  	
  	//确定充值
  	document.getElementById('sureChange').addEventListener('tap', function() {
  		if(a1_money<100){
  			mui.toast('余额小于100，不能充值');
  			return ;
  		}
  		var pwd=document.getElementById('transact_pwd').value.trim();
  		if(pwd==''){
  			mui.toast('交易密码不能为空');
  			return ;
  		}
  		  mui.ajax({
					url: apiurl + "Shift/do_pay",
					data: {'trade_pwd':pwd, '_token':userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'post',
					timeout: 10000,
					success: function(res) {
						//console.log(JSON.stringify(res));
						// 请求成功
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}else if(res.status==0){
							mui.toast(res.msg);
						 	return false;
						}
						else{
							document.getElementById('balance').innerHTML=res.data.a1_money;
							a1_money=res.data.a1_money;
							
							document.getElementById('pay_card').innerHTML=res.data.pay_card_data.card;
							document.getElementById('pay_pwd').innerHTML=res.data.pay_card_data.password;
							//mui("#popover").popover('show');
							mui("#popover").popover('toggle', document.getElementById("div"));
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
  	});
  	
  	
  	//一键复制
  	document.getElementById('btnCopy').addEventListener('tap', function() {
  		var cope_str="充值卡号："+document.getElementById('pay_card').innerHTML+"\n"+"充值密码："+document.getElementById('pay_pwd').innerHTML;
  		if(mui.os.android) {
  			var Context = plus.android.importClass("android.content.Context");
			var main = plus.android.runtimeMainActivity();
			var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
			plus.android.invoke(clip,"setText",cope_str);
						
		}
		if(mui.os.ios) {
			var UIPasteboard  = plus.ios.importClass("UIPasteboard");
			var generalPasteboard = UIPasteboard.generalPasteboard();
			// 设置文本内容:
			generalPasteboard.setValueforPasteboardType("testValue",cope_str);
		}
					
		mui.toast('复制充值卡号和充值密码成功!');	
		document.getElementById('pay_card').innerHTML='';
		document.getElementById('pay_pwd').innerHTML='';
		document.getElementById('transact_pwd').value='';
  		mui("#popover").popover('hide');
  	});
  	
})

  

	//加载数据
    function refresh_data(){
        mui.ajax({
					url: apiurl + "User/index",
					data: {'_token':userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						//console.log(JSON.stringify(res));
						// 请求成功
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}else {
							document.getElementById('balance').innerHTML=res.data.a1_money;
							document.getElementById('user_name').value=res.data.user_name;
							a1_money=res.data.a1_money;
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
		}